--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Position");
Use("common/Task");
Use("common/Creature");
Use("common/Pet");
Use("common/Skill");

-- The archer persona overrides some of the default persona behavior
-- for the archer's more long-range low-health properties.
local name = "Archer";
local Initialize, status = loadfile(PERSONALITY_DEFAULT);
if (Initialize == nil) then
  Alert(name .. ": " .. status);
  return ME;
end

Archer = {};

--[[ React to the current situation ]]--
--  We may go into an avoidance mode if we get too hurt.
function Archer.React(self)
  local health = GetV(V_HP, self.id);
  local threshold = math.floor(self.maxhp / self:GetResilience());
  if ((self.task.hp >= threshold) and (health < threshold)) then
    -- We've taken significant damage since our last task began. Try to avoid enemies
    -- until the owner gives further instruction.
    -- TODO: ideally, we emote.
    Info(name .. ": React() Retreat at " .. health .. "/" .. self.maxhp);
    self:Behave(MODE_AVOID);
  else
     -- Fall back to default behavior.
    if (Persona.React ~= nil) then Persona.React(self) end
  end
end

--[[ Override Default Rating Calculation ]]--
-- Archers are more range-sensitive, and must consider their
-- own survival as a higher priority.
function Archer.RateTarget(self, _id)
  local target = GetV(V_TARGET, _id);
  local pettarget = GetV(V_TARGET, self.id);
  local ownertarget = GetV(V_TARGET, self.owner.id);
  local score = Pet.RateTarget(self, _id);

  -- If he's attacking me, give bonus.
  if (target == self.id) then score = score + 8 / self:GetTranquility() end
  -- If he's attacking the owner, give bonus.
  if (target == self.owner.id) then score = score + 6 / self:GetTranquility() end
  -- If he's my current target, give bonus.
  if (pettarget == _id) then score = score + 4 / self:GetTranquility() end
  -- If he's my owner's target, give bonus.
  if (ownertarget == _id) then score = score + 2 / self:GetTranquility() end

  -- Now check against friendly creatures (minimal assist).
  local key, value;
  for key, value in ipairs(self.friends) do
    local friendtarget = GetV(V_TARGET, key);
    if (target == key) then score = score + 1 / self:GetTranquility() end
    if (friendtarget == _id) then score = score + 1 / self:GetTranquility() end
  end

  return score;
end

--[[ Archers are more tranquil than others ]]--
function Archer.GetTranquility(self)
  local value = TRANQUILITY + 1;
  if (value < 3) then value = 3 end
  return value;
end
--[[ Archers are less resilient than others ]]--
function Archer.GetResilience(self)
  local value = RESILIENCE - 1;
  if (value < 2) then value = 2 end
  return value;
end



-- Initialize base personality.
--local instance = Initialize(...);
--local instance = Initialize(arg[1]);
local instance = Initialize();

instance.React = Archer.React;
instance.RateTarget = Archer.RateTarget;
instance.GetTranquility = Archer.GetTranquility;
instance.GetResilience = Archer.GetResilience;

Info(name .. " Loaded");
return instance;
